﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.WinUI.Bpt;

namespace ESOL.WinUI.Bpt
{
    public partial class frmMaintainProductQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstProductEntity mSelectedProduct;
        public MstProductEntity SelectedProduct
        {
            get { return mSelectedProduct; }
        }
        private MstProductTypeEntity mSelectedProductType;
        List<string> Listcondition = new List<string>();
        List<string> ListProductId = new List<string>();
        public string sCondition { set; get; }
        #endregion

        #region Constructor
        public frmMaintainProductQuery()
        {
            InitializeComponent();
        }
        public frmMaintainProductQuery(MstProductTypeEntity productType)
        {
            InitializeComponent();
            mSelectedProductType = productType;
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtBroadClassName.Text) == false)
            {
                condition += " AND C.PRODUCT_TYPE_NAME LIKE '%" + txtBroadClassName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtBroadClassCode.Text) == false)
            {
                condition += " AND C.PRODUCT_TYPE_CODE LIKE '%" + txtBroadClassCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMiddleClassName.Text) == false)
            {
                condition += " AND B.PRODUCT_TYPE_NAME LIKE '%" + txtMiddleClassName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMiddleClassCode.Text) == false)
            {
                condition += " AND B.PRODUCT_TYPE_CODE LIKE '%" + txtMiddleClassCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND A.TYPE_NAME LIKE '%" + txtTypeName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND A.TYPE_CODE LIKE '%" + txtTypeCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtStandardName.Text) == false)
            {
                condition += " AND A.STD_NAME LIKE '%" + txtStandardName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtStandardCode.Text) == false)
            {
                condition += " AND A.STD_CODE LIKE '%" + txtStandardCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMaterialName.Text) == false)
            {
                condition += " AND A.MATERIAL LIKE '%" + txtMaterialName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMaterialCode.Text) == false)
            {
                condition += " AND A.MATERIAL_CODE LIKE '%" + txtMaterialCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductCode.Text) == false)
            {
                condition += " AND A.PRODUCT_CODE LIKE '%" + txtProductCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtProductName.Text) == false)
            {
                condition += " AND A.PRODUCT_NAME LIKE '%" + txtProductName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtRemark.Text) == false)
            {
                condition += " AND A.DESCRIPTION LIKE '%" + txtRemark.Text + "%'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            if (string.IsNullOrEmpty(sCondition) == false)
            {
                condition += sCondition;
            }
            string rowCount = txtRowCount.Text;
            string orderBy = "PRODUCT_CODE";

            DataTable dt = MstProductBs.GetByCondition_Code(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProduct = new MstProductEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedProduct == null)
            {
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        protected override void Click_Confirm()
        {
            if (mSelectedProduct == null)
            {
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtBroadClassName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "BroadClassName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtBroadClassName.Text = frm.aList[0];
                txtBroadClassCode.Text = frm.aList[1];
            }
            else
            {
                txtBroadClassName.Text = "";
                txtBroadClassCode.Text = "";
            }
        }

        private void txtBroadClassCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "BroadClassCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtBroadClassName.Text = frm.aList[0];
                txtBroadClassCode.Text = frm.aList[1];
            }
            else
            {
                txtBroadClassCode.Text = "";
                txtBroadClassName.Text = "";
            }
        }

        private void txtMiddleClassName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MiddleClassName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMiddleClassName.Text = frm.aList[0];
                txtMiddleClassCode.Text = frm.aList[1];
            }
            else
            {
                txtMiddleClassName.Text = "";
                txtMiddleClassCode.Text = "";
            }
        }

        private void txtMiddleClassCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MiddleClassCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMiddleClassName.Text = frm.aList[0];
                txtMiddleClassCode.Text = frm.aList[1];
            }
            else
            {
                txtMiddleClassName.Text = "";
                txtMiddleClassCode.Text = "";
            }
        }

        private void txtTypeName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "TypeName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtTypeName.Text = frm.aList[0];
                txtTypeCode.Text = frm.aList[1];
            }
            else
            {
                txtTypeName.Text = "";
                txtTypeCode.Text = "";
            }
        }

        private void txtTypeCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "TypeCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtTypeName.Text = frm.aList[0];
                txtTypeCode.Text = frm.aList[1];
            }
            else
            {
                txtTypeName.Text = "";
                txtTypeCode.Text = "";
            }
        }

        private void txtStandardName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "StdName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtStandardName.Text = frm.aList[0];
                txtStandardCode.Text = frm.aList[1];
            }
            else
            {
                txtStandardName.Text = "";
                txtStandardCode.Text = "";
            }
        }

        private void txtStandardCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "StdCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtStandardName.Text = frm.aList[0];
                txtStandardCode.Text = frm.aList[1];
            }
            else
            {
                txtStandardName.Text = "";
                txtStandardCode.Text = "";
            }
        }

        private void txtMaterialName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MaterialName";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMaterialName.Text = frm.aList[0];
                txtMaterialCode.Text = frm.aList[1];
            }
            else
            {
                txtMaterialName.Text = "";
                txtMaterialCode.Text = "";
            }
        }

        private void txtMaterialCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "MaterialCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtMaterialName.Text = frm.aList[0];
                txtMaterialCode.Text = frm.aList[1];
            }
            else
            {
                txtMaterialName.Text = "";
                txtMaterialCode.Text = "";
            }
        }

        private void txtProductName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            Listcondition.Add(txtProductName.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "ProductCode";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtProductName.Text = frm.aList[0];
                txtProductCode.Text = frm.aList[1];
            }
            else
            {
                txtProductName.Text = "";
                txtProductCode.Text = "自动生成";
            }
        }

        private void txtRemark_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Listcondition = new List<string>();
            Listcondition.Add(txtBroadClassName.Text);
            Listcondition.Add(txtBroadClassCode.Text);
            Listcondition.Add(txtMiddleClassName.Text);
            Listcondition.Add(txtMiddleClassCode.Text);
            Listcondition.Add(txtTypeName.Text);
            Listcondition.Add(txtTypeCode.Text);
            Listcondition.Add(txtStandardName.Text);
            Listcondition.Add(txtStandardCode.Text);
            Listcondition.Add(txtMaterialName.Text);
            Listcondition.Add(txtMaterialCode.Text);
            Listcondition.Add(txtProductName.Text);
            Listcondition.Add(txtRemark.Text);
            frmMaintainEncoderQuery frm = new frmMaintainEncoderQuery();
            frm.ListIn = Listcondition;
            frm.SType = "Remark";
            if (frm.ShowDialog() == DialogResult.OK && frm.aList != null)
            {
                txtProductName.Text = frm.aList[0];
                txtProductCode.Text = frm.aList[1];
            }
            else
            {
                //txtProductName.Text = "";
                txtProductCode.Text = "自动生成";
            }
        }

        #endregion
    }
}
